package cn.gdcp.myapplication;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity1 extends AppCompatActivity {
    private TextView textView;
    private ImageView imageView;
    private Button button;
    private int index;
    private int []arr={R.drawable.as1,R.drawable.as2,R.drawable.as3,R.drawable.as4};
    private Handler handler;
    private final int Img_chaange=1001;
    private boolean isPase=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);

        textView =(TextView)findViewById(R.id.text_1);
        imageView =(ImageView)findViewById(R.id.image_view);
        button =(Button)findViewById(R.id.button);



        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if(msg.what==Img_chaange){
                    index=(index+1)%arr.length;
                    imageView.setImageResource(arr[index]);
                }
                return false;
            }
        });
               new Thread(new Runnable() {
                   @Override
                   public void run() {
                       while(true) {
                           try {
                               Thread.sleep(2000);
                           } catch (InterruptedException e) {
                               e.printStackTrace();
                           }
                           if (!isPase) {
                               Message message = new Message();
                               message.what = Img_chaange;
                               handler.sendMessage(message);
                           }
                       }
                   }
               }).start();
               button.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       isPase=!isPase;
                   }
               });
            }

}
